<template>
  <div>
    <el-form class="query-table-form" @submit.native.prevent :inline="true" :model="params">
      <el-form-item>
        <el-input
          v-model="params.customer_name"
          placeholder="项目名称查询"
          clearable
          @change="getData"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="params.corp_name"
          placeholder="负责人查询"
          clearable
          @change="getData"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <select-address
          v-model="params.address_code"
          placeholder="地址查询"
          auto-top
          clearable
          @change="getData"
        ></select-address>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="getData()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button
          icon="el-icon-download"
          :loading="exportLoading"
          @click="exportExcel('联保复审（已复审）')"
        >
          导出
        </el-button>
      </el-form-item>
    </el-form>
    <c-table border v-loading="loading" :columns="tableColumns" :data="tableData">
      <template v-slot:address="scope">
        <el-tooltip effect="light" :content="scope.row.address">
          <div class="text-truncate">{{ scope.row.address }}</div>
        </el-tooltip>
      </template>
      <template v-slot:itemStatus="scope">
        <span class="badge" :class="`badge-${scope.row.itemStatusStyle}`">
          {{ scope.row.itemStatusLabel }}
        </span>
      </template>
    </c-table>
    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="totalCount"
        @current-change="getData"
      ></el-pagination>
    </div>

    <attach-cmpt :visible.sync="attachVisible" :customer-related-id="attachRelatedId"></attach-cmpt>

    <el-dialog
      :visible.sync="reportVisible"
      :close-on-click-modal="false"
      append-to-body
      top="30px"
      width="80%"
    >
      <el-tabs>
        <el-tab-pane label="调查报告">
          <credit-file :query-id="queryId" :viewMode="true"></credit-file>
        </el-tab-pane>
        <el-tab-pane label="担保意向函" v-if="+relatedData.ele_letter_status > 0">
          <letter-view :related-data="relatedData"></letter-view>
        </el-tab-pane>
        <el-tab-pane label="联保函">
          <union-letter :related-data="relatedData" :isView="true"></union-letter>
        </el-tab-pane>
        <el-tab-pane label="联担审批表">
          <review-credit-view :relatedData="relatedData"></review-credit-view>
        </el-tab-pane>
        <el-tab-pane label="附件">
          <attach-view :queryId="queryId" :visible="reportVisible"></attach-view>
        </el-tab-pane>
      </el-tabs>
    </el-dialog>
    <el-dialog
      :visible.sync="visible"
      :close-on-click-modal="false"
      append-to-body
      top="30px"
      width="800px"
    >
      <union-letter @success="handleEditSuccess" :related-data="related"></union-letter>
    </el-dialog>
  </div>
</template>

<script>
import CreditFile from '@/credit/components/creditFile/InvestigateReport'
import AttachView from '@/credit/components/AttachView'
import taskTableMixin from '@/credit/mixin/mission/taskTable'
import SelectAddress from '@/components/select/SelectAddress'
import { prelimTableColumns } from '@/credit/views/city/mission/ProjectReviewIng'
import { enumGuatype, enumProjectEleStatus } from '@/credit/plugins/enumTown'
import AttachCmpt from '@/credit/views/city/program/Attach'
import ReviewCreditView from '@/credit/views/county/mission/components/ReviewCreditView'
import LetterView from '@/credit/views/county/mission/components/LetterView'
import UnionLetter from '@/credit/views/county/mission/components/UnionLetter'
export default {
  components: {
    SelectAddress,
    CreditFile,
    AttachView,
    AttachCmpt,
    LetterView,
    ReviewCreditView,
    UnionLetter,
  },
  mixins: [taskTableMixin],
  props: {},
  data() {
    return {
      enumProjectEleStatus,
      enumGuatype,
      order: {
        N1res_date_corp: 'desc',
        N2create_time: 'desc',
      },
      params: {
        index: 1,
        size: 15,
        'project_res_status.eq': '2',
        'city_project_res_status.eq': '2',
        'gua_type.eq': '1',
        address_code: '',
        customer_name: '',
        corp_name: '',
      },
      tableName: 'customer_related',
      tableData: [],
      tableColumns: [
        ...prelimTableColumns,
        {
          attrs: { label: '调查完成时间', prop: 'resTime', align: 'center', width: 140 },
          process: v => v || '-',
        },
        {
          attrs: { label: '合作银行', prop: 'bank_name', align: 'center' },
          process: v => v || '-',
        },
        {
          attrs: { label: '联保函状态', prop: 'itemStatusLabel', align: 'center' },
          slot: 'itemStatus',
        },
        {
          attrs: { label: '操作', align: 'center', width: 150 },
          operations: [
            {
              content: '联保函回执',
              icon: 'fa fa-gavel',
              event: this.handleEdit,
              ifRender: row => +row.gua_letter_status === 1,
            },
            { content: '上传附件', icon: 'fa fa-clipboard', event: this.hanleAttach },
            { content: '查看', icon: 'fa fa-search', event: this.handleView },
          ],
        },
      ],
      attachVisible: false,
      attachRelatedId: '',
      queryId: '',
      relatedData: {},
      reportVisible: false,
      related: {},
      visible: false,
    }
  },
  computed: {},
  watch: {},
  mounted() {},
  created() {
    this.getData()
  },
  methods: {
    handleEdit(row) {
      this.related = { ...row }
      this.visible = true
    },
    handleEditSuccess() {
      this.visible = false
      this.getData()
    },
    hanleAttach(row) {
      this.attachVisible = true
      this.attachRelatedId = row.customer_related_id
    },
    handleView(row) {
      this.queryId = row.customer_related_id
      this.relatedData = row
      this.reportVisible = true
    },
    dataAdapter(item) {
      item.guaType = enumGuatype[item.gua_type] || '-'
      item.resTime = this.$utils.dateFormat(item.res_date_corp, this.dateFmt)
      const itemStatus = enumProjectEleStatus[item.gua_letter_status] || {}
      item.itemStatusLabel = itemStatus.label
      item.itemStatusStyle = itemStatus.css

      return item
    },
  },
}
</script>
<style lang="scss" scoped></style>
